package cn.iocoder.yudao.module.crm.service.wgm;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.crm.controller.admin.wgm.vo.*;
import cn.iocoder.yudao.module.crm.dal.dataobject.wgm.WgmDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.crm.dal.mysql.wgm.WgmMapper;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.*;

/**
 * 外购项目 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WgmServiceImpl implements WgmService {

    @Resource
    private WgmMapper wgmMapper;

    @Override
    public Long createWgm(WgmSaveReqVO createReqVO) {
        // 插入
        WgmDO wgm = BeanUtils.toBean(createReqVO, WgmDO.class);
        wgmMapper.insert(wgm);
        // 返回
        return wgm.getId();
    }

    @Override
    public void updateWgm(WgmSaveReqVO updateReqVO) {
        // 校验存在
        validateWgmExists(updateReqVO.getId());
        // 更新
        WgmDO updateObj = BeanUtils.toBean(updateReqVO, WgmDO.class);
        wgmMapper.updateById(updateObj);
    }

    @Override
    public void deleteWgm(Long id) {
        // 校验存在
        validateWgmExists(id);
        // 删除
        wgmMapper.deleteById(id);
    }

    private void validateWgmExists(Long id) {
        if (wgmMapper.selectById(id) == null) {
            throw exception(WGM_NOT_EXISTS);
        }
    }

    @Override
    public WgmDO getWgm(Long id) {
        return wgmMapper.selectById(id);
    }

    @Override
    public PageResult<WgmDO> getWgmPage(WgmPageReqVO pageReqVO) {
        return wgmMapper.selectPage(pageReqVO);
    }

}